package com.zs.studentmanager.controller;

import com.zs.studentmanager.common.ResponseCodeEnum;
import com.zs.studentmanager.common.Result;
import com.zs.studentmanager.entity.domain.Role;
import com.zs.studentmanager.entity.vo.PageVo;
import com.zs.studentmanager.service.RoleService;
import com.zs.studentmanager.utils.CommonUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author: 周小白
 * @date: 2024-01-21 14:12
 **/
@RestController
@RequestMapping("/role")
@Tag(name = "角色管理")
public class RoleController {

    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }





    /**
     * 获取角色列表
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param searchValue 搜索值
     * @return 角色列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取角色列表")
    public Result getRoleList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                              @RequestParam(value = "searchValue", required = false) String searchValue) {
        // 参数校验
        if (pageNum < 1 || pageSize < 1) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        //  查询
        PageVo<Role> pageVo = roleService.getPageList(pageNum, pageSize, searchValue);

        if (pageVo == null) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }


        return Result.success(pageVo);
    }





    /**
     * 添加角色
     * @param role 角色信息
     * @return 添加结果
     */
    @PostMapping("/add")
    @Operation(summary = "添加角色")
    @Transactional(rollbackFor = Exception.class)
    public Result addRole(@RequestBody Role role) {
        // 参数校验
        if (role == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        role.setCreateTime(LocalDateTime.now());
        long currentUserId = CommonUtils.getCurrentUserId();
        role.setCreateBy(currentUserId);

        // 添加
        boolean result = roleService.save(role);
        if (!result) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success();
    }


    /**
     * 获取角色详情
     * @param id 角色id
     * @return 角色详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取角色详情")
    public Result getRoleDetail(@PathVariable("id") Long id) {
        if (id == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        Role role = roleService.getById(id);

        return Result.success(role);
    }





    /**
     * 修改角色
     * @param role 角色信息
     * @return 修改结果
     */
    @PutMapping("/update")
    @Operation(summary = "修改角色")
    @Transactional(rollbackFor = Exception.class)
    public Result updateRole(@RequestBody Role role) {
        // 参数校验
        if (role == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        role.setUpdateTime(LocalDateTime.now());
        long currentUserId = CommonUtils.getCurrentUserId();
        role.setUpdateBy(currentUserId);

        // 修改
        boolean result = roleService.updateById(role);
        if (!result) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success();
    }





    /**
     * 删除角色
     * @param id 角色id
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除角色")
    @Transactional(rollbackFor = Exception.class)
    public Result deleteRole(@PathVariable("id") Long id) {
        if (id == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        // 删除
        boolean result = roleService.removeById(id);
        if (!result) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success();
    }
}
